package com.damie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dadie.Domain.SiteDomain;
import com.dadie.Dto.SiteDto;
import com.dadie.parame.SiteParame;
import com.damie.mapper.SiteMapper;
import com.damie.service.SiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-21
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, SiteDomain> implements SiteService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private SiteMapper mapper;

    @Override
    public Page<SiteDto> selectAllAddSite(Integer index,Integer size,SiteParame siteParame) {
        QueryWrapper<SiteDomain> wrapper = new QueryWrapper<>();
        wrapper.eq("site_status",0);
        wrapper.orderByAsc("create_time");
        Page<SiteDomain> siteDomainPage = new Page<>(index, size);
        Page<SiteDomain> siteDomains = mapper.selectPage(siteDomainPage, wrapper);
        List<SiteDto> collect = siteDomains.getRecords().stream().map(siteDomain -> {
            SiteDto siteDto = new SiteDto();
            BeanUtils.copyProperties(siteDomain, siteDto);
            return siteDto;
        }).collect(Collectors.toList());
        Page<SiteDto> page = new Page<>();
        BeanUtils.copyProperties(siteDomainPage,page);
        page.setRecords(collect);
        return page;
    }

    @Override
    public Page<SiteDto> selectAllSite(Integer index, Integer size,SiteParame siteParame) {
        QueryWrapper<SiteDomain> wrapper = new QueryWrapper<>();
        wrapper.eq("site_status",1);
        if (siteParame.getSiteName()!=null&&!siteParame.getSiteName().equals("")){
            wrapper.like("site_name",siteParame.getSiteName());
        }
        if (siteParame.getSiteType()!=null){
            wrapper.eq("site_type",siteParame.getSiteType());
        }
        Page<SiteDomain> siteDomainPage = new Page<>(index, size);
        Page<SiteDomain> siteDomains = mapper.selectPage(siteDomainPage,wrapper);
        List<SiteDto> collect = siteDomains.getRecords().stream().map(siteDomain -> {
            SiteDto siteDto = new SiteDto();
            BeanUtils.copyProperties(siteDomain, siteDto);
            return siteDto;
        }).collect(Collectors.toList());
        Page<SiteDto> page = new Page<>();
        BeanUtils.copyProperties(siteDomainPage,page);
        page.setRecords(collect);
        return page;
    }

    @Override
    public SiteDto selectByAddSite(String siteId) {
        SiteDomain siteDomain = mapper.selectById(siteId);
        if (ObjectUtils.isNotEmpty(siteDomain)){
            SiteDto siteDto = new SiteDto();
            BeanUtils.copyProperties(siteDomain,siteDto);
            return siteDto;
        }
        throw new RuntimeException("查询出错");
    }

    @Override
    public SiteDto delete(String siteId) {
        QueryWrapper<SiteDomain> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("site_id",siteId);
        SiteDomain siteDomain = mapper.selectOne(objectQueryWrapper);
        if (ObjectUtils.isNotEmpty(siteDomain)){
            //如果查询的申请站点id有的话判断状态
            if (siteDomain.getSiteStatus()!=1){
                //说明是正在删除的站点

                mapper.deleteById(siteId);
                SiteDto siteDto = new SiteDto();
                BeanUtils.copyProperties(siteDomain,siteDto);
                return  siteDto;
            }
        }
        throw  new RuntimeException("删除出错");
    }

    @Override
    @Transactional
    public SiteDto approved(String siteId) {
        QueryWrapper<SiteDomain> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("site_id",siteId);
        SiteDomain siteDomain = mapper.selectOne(objectQueryWrapper);
        if (ObjectUtils.isNotEmpty(siteDomain)){
            //如果查询的申请站点id有的话判断状态
            if (siteDomain.getSiteStatus()!=1){
               //说明是正在申请的站点
               siteDomain.setSiteStatus(1);
               mapper.updateById(siteDomain);
               SiteDto siteDto = new SiteDto();
               BeanUtils.copyProperties(siteDomain,siteDto);
               return  siteDto;
            }
        }
        throw  new RuntimeException("修改出错");
    }

    @Override
    public SiteDto updateByIds(SiteParame siteParame) {
        SiteDomain siteDomain = mapper.selectById(siteParame.getSiteId());
        if (ObjectUtils.isNotEmpty(siteDomain)){
            if (siteDomain.getSiteStatus()!=0){
                siteDomain.setSiteName(siteParame.getSiteName());
                siteDomain.setSiteTel(siteParame.getSiteTel());
                siteDomain.setSiteType(siteParame.getSiteType());
                mapper.updateById(siteDomain);
            }
        }
        throw new RuntimeException("修改出错");
    }
}
